package com.jjmmbb.db.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jjmmbb.anydolist.Comm;
import com.jjmmbb.anydolist.R;
import com.jjmmbb.anydolist.Util;
import com.jjmmbb.db.pojo.Enums;
import com.jjmmbb.db.pojo.Item;

public class MainAdapter extends BaseExpandableListAdapter {
	Context context;

	private ArrayList<Item> list;

	public OptionListener optionListener;

	public MainAdapter(Context c, ArrayList<Item> list) {
		this.context = c;
		this.list = list;
	}

	@Override
	public int getGroupCount() {
		return list.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return 1;
	}

	@Override
	public Object getGroup(int groupPosition) {
		return groupPosition;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return null;
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {

		Item item = list.get(groupPosition);
		TextView textView;
		if (null == convertView) {
			textView = new TextView(context);
			textView.setPadding(10, 15, 10, 15);
			textView.setTextAppearance(context, R.style.Medium);
			textView.setBackgroundResource(R.drawable.btn_f);
			textView.setSingleLine();
			textView.setEllipsize(TextUtils.TruncateAt.END);
		} else {
			textView = (TextView) convertView;
		}

		String msg = "";
		SpannableString ss;
		int indexEnter = item.title.indexOf("\n");
		String title = item.title;
		if (indexEnter > 0) {
			title = title.substring(0, indexEnter);
		}
		if (!Comm.hide_team && !Util.isNull(item.team)) {
			msg += "[" + item.team + "] ";
			int length = msg.length();
			msg += title;

			// 给分组文本添加颜色
			ss = new SpannableString(msg);
			ss.setSpan(new ForegroundColorSpan(Color.GRAY), 0, length - 1,
					Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
			textView.setText(ss);
		} else {
			msg += title;
			textView.setText(msg);
		}

		switch (item.type) {
		case Enums.Item_Doing:
			textView.setTextColor(context.getResources().getColor(
					R.color.dodgerblue));
			System.out.println(item.typeAlarm);
			if (item.typeAlarm <= Enums.Alarm_No || item.typeAlarm == 0) {
				textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
			} else {
				textView.setCompoundDrawablesWithIntrinsicBounds(0, 0,
						R.drawable.icon_doing, 0);
			}
			break;
		case Enums.Item_Finish:
			textView.setTextColor(context.getResources().getColor(
					R.color.sandybrown));
			textView.setCompoundDrawablesWithIntrinsicBounds(0, 0,
					R.drawable.icon_finish, 0);
			break;
		case Enums.Item_Delete:
			textView.setTextColor(context.getResources().getColor(
					R.color.darkcyan));
			textView.setCompoundDrawablesWithIntrinsicBounds(0, 0,
					R.drawable.icon_delete, 0);
			break;
		}

		return textView;
	}

	@Override
	public View getChildView(final int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		LinearLayout ll;
		if (null == convertView) {
			ll = (LinearLayout) LayoutInflater.from(context).inflate(
					R.layout.item_option, null);
		} else {
			ll = (LinearLayout) convertView;
		}
		ImageButton btCopy = (ImageButton) ll
				.findViewById(R.id.item_option_copy);
		ImageButton btDelete = (ImageButton) ll
				.findViewById(R.id.item_option_delete);
		ImageButton btDoing = (ImageButton) ll
				.findViewById(R.id.item_option_doing);
		ImageButton btFinish = (ImageButton) ll
				.findViewById(R.id.item_option_finish);
		ImageButton btEdit = (ImageButton) ll
				.findViewById(R.id.item_option_edit);
		ImageButton btFirst = (ImageButton) ll
				.findViewById(R.id.item_option_first);
		ImageButton btAutoLink = (ImageButton) ll
				.findViewById(R.id.item_option_autolink);

		Item item = list.get(groupPosition);
		switch (item.type) {
		case Enums.Item_Doing:
			btFinish.setVisibility(View.VISIBLE);
			btDoing.setVisibility(View.GONE);
			btDelete.setVisibility(View.GONE);
			btFirst.setVisibility(View.VISIBLE);
			break;
		case Enums.Item_Finish:
			btFinish.setVisibility(View.GONE);
			btDoing.setVisibility(View.VISIBLE);
			btDelete.setVisibility(View.VISIBLE);
			btFirst.setVisibility(View.GONE);
			break;
		}
		if (Util.canAutoLink(item.title)) {
			btAutoLink.setVisibility(View.VISIBLE);
		} else {
			btAutoLink.setVisibility(View.GONE);
		}

		btCopy.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (null != optionListener) {
					optionListener.onCopy(groupPosition);
				}
			}
		});
		btDelete.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (null != optionListener) {
					optionListener.onDelete(groupPosition);
				}
			}
		});
		btDoing.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (null != optionListener) {
					optionListener.onDoing(groupPosition);
				}
			}
		});
		btFinish.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (null != optionListener) {
					optionListener.onFinish(groupPosition);
				}
			}
		});
		btEdit.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (null != optionListener) {
					optionListener.onEdit(groupPosition);
				}
			}
		});
		btFirst.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (null != optionListener) {
					optionListener.onFirst(groupPosition);
				}
			}
		});
		btAutoLink.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (null != optionListener) {
					optionListener.onAutoLink(groupPosition);
				}
			}
		});

		return ll;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return false;
	}

	public void setOptionListener(OptionListener optionListener) {
		this.optionListener = optionListener;
	}

	// 操作接口
	public interface OptionListener {
		public void onCopy(int groupID);

		public void onDelete(int groupID);

		public void onFinish(int groupID);

		public void onDoing(int groupID);

		public void onFirst(int groupID);

		public void onEdit(int groupID);

		public void onAutoLink(int groupID);
	}

}
